package com.cooper.controllers.buyer;

import org.springframework.beans.factory.annotation.Autowired;

import com.cooper.constants.ResourceConstants;
import com.cooper.controllers.Controller;
import com.cooper.controllers.LoginRequired;
import com.cooper.core.JsonResult;
import com.cooper.entity.SendAddressEntity;
import com.cooper.entity.UserInfo;
import com.cooper.service.account.SendAddressService;
import com.cooper.util.BeanUtils;
import com.cooper.util.Controllers;
import com.cooper.vo.SendAddressVo;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

@LoginRequired
public class AddressController extends Controller {
	
	@Autowired
	private SendAddressService sendAddressService;
	
	@Get
	public String execute(Invocation inv) throws Exception{
		UserInfo userInfo = Controllers.getUserInfo(inv);
		inv.addModel("entityList", this.sendAddressService.findByOriginAndOuterId(ResourceConstants.AddressOrigin.RECEIVE_ADDRESS, userInfo.getId()));
		return "AddressMain";
	}
	
	@Get("edit")
	public String edit(Invocation inv, @Param("id") Long addressId) throws Exception{
		if(null != addressId){
			inv.addModel("entity", this.sendAddressService.loadById(addressId));
		}
		return "AddressEdit";
	}
	
	@Post("save")
	public Object save(Invocation inv, SendAddressVo addressVo, @Param("lang") String lang){
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		boolean result = false;
		
		try{
			if(null == addressVo.getId() || 0 == addressVo.getId()){
				Long id = this.sendAddressService.saveAddressVo(addressVo, userInfo, 
						ResourceConstants.AddressOrigin.RECEIVE_ADDRESS, lang);
				if(null != id && 0 < id){
					result = true;
				}
			} else {
				SendAddressEntity address = this.sendAddressService.loadById(addressVo.getId());
				BeanUtils.assembleUpdatePropertiesForEntity(addressVo, address);
				Controllers.prepareEntityForDataSaving(address, userInfo, true);
				result = this.sendAddressService.updateEntity(address);
			}
		} catch (Exception e){
			logger.error("Error:", e);
			json.setMessage(e.getMessage());
		} finally{
			json.setSuccess(result);
			if(false == result && null == json.getMessage()){
				json.setMessage("UNKNOWN ERROR!");
			}
		}
		return json;
	}
	
	@Post("delete")
	public Object delete(Invocation inv, @Param("id") Long addressId) {
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		SendAddressEntity entity = new SendAddressEntity();
		Controllers.prepareEntityForDataSaving(entity, userInfo, true);
		try {
			entity.setId(addressId);
			boolean result = this.sendAddressService.deleteEntity(entity);
			json.setSuccess(result);
		} catch (Exception e) {
			json.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}
	
	@Post("setAsDefault")
	public Object setAsDefault(Invocation inv, @Param("id") Long addressId,
			@Param("result") boolean result) {
		JsonResult json = new JsonResult();
		UserInfo userInfo = Controllers.getUserInfo(inv);
		SendAddressEntity entity = new SendAddressEntity();
		Controllers.prepareEntityForDataSaving(entity, userInfo, true);
		try {
			this.sendAddressService.setAsDefault(ResourceConstants.AddressOrigin.RECEIVE_ADDRESS, 
					userInfo.getId(), addressId, result);
			json.setSuccess(true);
		} catch (Exception e) {
			json.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return json;
	}

}
